package com.cliff.joinme;

import java.io.File;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.AQUtility;
import com.cliff.joinme.toolkit.ControlPointManager;
import com.cliff.joinme.toolkit.MediaStoreHelper;
import com.cliff.joinme.toolkit.data.MediaItem;
import com.cliff.joinme.toolkit.data.MusicItem;
import com.cliff.joinme.toolkit.data.VideoItem;

public class BrowseVideoActivity extends BrowseBaseActivity implements OnItemClickListener {

	protected AQuery listAq = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browsevideo);

		itemList = MediaStoreHelper.getInstance().getVideoItemList(this);
		initListView();				

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}


	protected void initListView() {
		AQUtility.cleanCacheAsync(this, 0, 0);
		BitmapAjaxCallback.clearCache();

		listAq = new AQuery(this);
		ArrayAdapter<MediaItem> aa = new ArrayAdapter<MediaItem>(this,
				android.R.layout.simple_list_item_1, itemList) {
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							android.R.layout.simple_list_item_1, parent, false);
				}
				VideoItem item = (VideoItem)getItem(position);
				AQuery listItemAq = listAq.recycle(convertView);
				listItemAq.id(android.R.id.text1).text(item.filePathName);
				return convertView;
			}
		};
		listAq.id(R.id.listViewVideo).adapter(aa);
		listAq.id(R.id.listViewVideo).itemClicked(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		if (null == ControlPointManager.getInstance().getSelectedRenderer()) {
			Intent intent = new Intent();  
			intent.setAction(android.content.Intent.ACTION_VIEW);
			File file = new File(itemList.get(position).filePathName);
			intent.setDataAndType(Uri.fromFile(file), "video/*");  
			startActivity(intent);
			return;
		}
		
        Intent intent = new Intent(BrowseVideoActivity.this, PlayVideoActivity.class);
        intent.putStringArrayListExtra(Constants.EXTRA_NAME_DATALIST, getFilePathNameList());
        intent.putExtra(Constants.EXTRA_NAME_POSITION, position);        
        startActivity(intent);		
	}	
	
}
